package ge.lowlevel;


import java.util.ArrayList;

import ge.modules.events.EventModule;

/**
 * 
 * @author Abel
 *	It's a Singleton class. It's not a low level engine.
 */
public class EventEngine 
{
	private ArrayList<EventModule> generators;
	private static EventEngine instance = null;
	private EventEngine()
	{
		generators = new ArrayList<EventModule>();
	}
	/**
	 * 	
	 * @return The singleton instance of Event engine
	 */
	public static EventEngine get()
	{
		if(instance == null)
			instance = new EventEngine();
		return instance;
	}
	/**
	 * Clears all the generators attached. It's called
	 * when a new level is loaded.
	 */
	public void clear()
	{
		generators.clear();
	}
	/**
	 * 	Attach a EventModule to the Event Engine
	 * @param module
	 */
	public void attach(EventModule module)
	{
		generators.add(module);
	}
	/**
	 * Removes the event module
	 * @param module Event Module
	 */
	public void remove(EventModule module)
	{
		generators.remove(module);
	}
	/**
	 * 	EventsListteners can ask to the event engine for a method.
	 * @param method	Name of the method
	 * @return	Object who has that method, null if it doesn't exits
	 */ 
	public EventModule ask(String method)
	{
		for(EventModule em : generators)
		{
			if(em.ask(method))
				return em;
		}
		return null;
	}
	
	/**
	 *  	EventsListteners can ask to the event engine for a method.
	 * @param method Name of the method
	 * @param id The id identify the EventModule between others with the same type.
	 * @return who has that method, null if it doesn't exits
	 */
	public EventModule ask(String method, int id)
	{
		for(EventModule em : generators)
		{
			if(em.ask(method) && (id == 0 || em.getId() == id))
				return em;
		}
		return null;
	}
	
	
}
